package com.example.m_homework4;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    Button btnBindService, btnUnbindService, btnGetStatus;
    TextView tvServiceStatus;
    MyService.MyServiceBinder serviceBinder;
    boolean isServiceBind = false;

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            serviceBinder = (MyService.MyServiceBinder) service;
            isServiceBind = true;
            Toast.makeText(MainActivity.this, "服务绑定成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isServiceBind = false;
            Toast.makeText(MainActivity.this, "服务异常断开", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnBindService = findViewById(R.id.btn_main_activity_bind_service);
        btnUnbindService = findViewById(R.id.btn_main_activity_unbind_service);
        btnGetStatus = findViewById(R.id.btn_main_activity_get_status);
        tvServiceStatus = findViewById(R.id.tv_main_activity_service_status);

        btnBindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                // 如果service尚未绑定就绑定，如果已经绑定则忽略
                if (!isServiceBind) {
                    bindService(intent, conn, Context.BIND_AUTO_CREATE);
                    tvServiceStatus.setText("Binding Service...");
                }
            }
        });
        btnUnbindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 如果service已经绑定，则可以解绑，否则忽略
                if (isServiceBind) {
                    unbindService(conn);
                    isServiceBind = false;
                    tvServiceStatus.setText("Unbinding Service...");
                }
            }
        });
        btnGetStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 如果service已经绑定，获取service的count计数并显示在截面上。
                if (isServiceBind && serviceBinder != null) {
                    int count = serviceBinder.getCount();
                    tvServiceStatus.setText("Current Count: " + count);
                } else {
                    tvServiceStatus.setText("Service not bound");
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isServiceBind) {
            unbindService(conn);
        }
    }
}